package com.xinghai.system.service.impl;

import java.util.Collections;
import java.util.List;
import com.xinghai.common.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import com.xinghai.system.mapper.JsBankMapper;
import com.xinghai.system.domain.JsBank;
import com.xinghai.system.service.IJsBankService;

/**
 * 银行Service业务层处理
 * 
 * @author xinghai
 * @date 2024-08-31
 */
@Service
public class JsBankServiceImpl implements IJsBankService 
{
    @Autowired
    private JsBankMapper jsBankMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询银行
     * 
     * @param id 银行主键
     * @return 银行
     */
    @Override
    public JsBank getInfo(Long id)
    {
        return jsBankMapper.getInfo(id);
    }

    /**
     * 查询银行列表
     * 
     * @param jsBank 银行
     * @return 银行
     */
    @Override
    public List<JsBank> list(JsBank jsBank)
    {
            jsBank.setDelFlag("0");
        return jsBankMapper.list(jsBank);
    }

    /**
     * 新增银行
     * 
     * @param jsBank 银行
     * @return 结果
     */
    @Override
    public int insert(JsBank jsBank)
    {
        jsBank.setCreateTime(DateUtils.getNowDate());
        return jsBankMapper.insert(jsBank);
    }
    /**
     * 批量新增银行
     *
     * @param jsBankList 银行
     * @return 结果
     */
    @Override
    public int insertBatch(List<JsBank> jsBankList)
    {
        return jsBankMapper.insertBatch(jsBankList);
    }
    /**
     * 修改银行
     * 
     * @param jsBank 银行
     * @return 结果
     */
    @Override
    public int update(JsBank jsBank)
    {
        jsBank.setUpdateTime(DateUtils.getNowDate());
        return jsBankMapper.update(jsBank);
    }

    /**
     * 批量删除银行
     * 
     * @param ids 需要删除的银行主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return jsBankMapper.deleteBatch(ids);
    }

    /**
     * 删除银行信息
     * 
     * @param id 银行主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return jsBankMapper.delete(id);
    }

    /**
     * 导入银行信息
     *
     * @param list 银行信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<JsBank> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            JsBankMapper jsBankMapper = sqlSession.getMapper(JsBankMapper.class);
            list.stream().forEach(item -> jsBankMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }

    @Override
    public List<JsBank> listByIds(String bankId) {
        return jsBankMapper.listByIds(bankId);
    }
}
